package com.huanwei.note;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;


import com.getbase.floatingactionbutton.FloatingActionButton;
import com.huanwei.cc.R;
import com.huanwei.cc.activity.EditNoteActivity;
import com.huanwei.cc.adapter.NoteAdapter;
import com.huanwei.cc.bean.MyEvent;
import com.huanwei.cc.bean.Note;
import com.huanwei.cc.db.DBManager;

import java.util.ArrayList;

//import adapter.MyAdapter;
//import bean.MyEvent;
//import bean.Note;
import butterknife.ButterKnife;
//import db.DBManager;
import de.greenrobot.event.EventBus;
//import life.com.activity.EditNoteActivity;
//import life.com.activity.R;

/**
 * 记事本
 */
public class Note_Fragment extends Fragment {
    private FloatingActionButton addBtn;
    private DBManager dm;
    private ArrayList<Note> noteDataList = new ArrayList<>();
    private NoteAdapter adapter;
    private ListView listView;
    private TextView emptyListTextView;
    long waitTime = 2000;
    long touchTime = 0;

    View view;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (view == null) {
            view = View.inflate(getActivity(), R.layout.fragment_xq, null);
            ButterKnife.bind(this, view);
            EventBus.getDefault().register(this);
            init();
        }
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null) {
            parent.removeView(view);
        }

        return view;
    }

    public void onEventMainThread(MyEvent event) {

        if (!TextUtils.isEmpty(event.getContent())) {
            Log.e("777", "8888");
                noteDataList.clear();
                dm.readFromDB(noteDataList);
                adapter.additem(noteDataList);
                updateView();


        }


    }

    //初始化
    private void init() {
        dm = new DBManager(getActivity());
        dm.readFromDB(noteDataList);
        listView = (ListView) view.findViewById(R.id.list);
        addBtn = (FloatingActionButton) view.findViewById(R.id.add);
        emptyListTextView = (TextView) view.findViewById(R.id.empty);
        addBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(getActivity(), EditNoteActivity.class);
                startActivity(i);
            }
        });
        adapter = new NoteAdapter(getActivity());
        listView.setAdapter(adapter);
        adapter.additem(noteDataList);
        listView.setOnItemClickListener(new NoteClickListener());
        listView.setOnItemLongClickListener(new NoteLongClickListener());

        updateView();
    }

    //空数据更新
    private void updateView() {
        if (noteDataList.isEmpty()) {
            listView.setVisibility(View.GONE);
            emptyListTextView.setVisibility(View.VISIBLE);
        } else {
            listView.setVisibility(View.VISIBLE);
            emptyListTextView.setVisibility(View.GONE);
        }
    }

    //listView单击事件
    private class NoteClickListener implements AdapterView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            NoteAdapter.ViewHolder viewHolder = (NoteAdapter.ViewHolder) view.getTag();
            String noteId = viewHolder.tvId.getText().toString().trim();
            Intent intent = new Intent(getActivity(), EditNoteActivity.class);
            intent.putExtra("id", Integer.parseInt(noteId));
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);

        }
    }

    //listView长按事件
    private class NoteLongClickListener implements AdapterView.OnItemLongClickListener {
        @Override
        public boolean onItemLongClick(final AdapterView<?> adapterView, View view, final int i, long l) {
            final Note note = ((NoteAdapter) adapterView.getAdapter()).getItem(i);
            if (note == null) {
                return true;
            }
            final int id = note.getId();
            new AlertDialog.Builder(getActivity()).setMessage("是否删除?")
                    .setCancelable(false)
                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {


                        }
                    }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    DBManager.getInstance(getActivity()).deleteNote(id);
                    adapter.removeItem(i);
                    updateView();
                }
            }).show();

            return true;
        }
    }

    // 取消注册
    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


}



